﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.ComponentModel.Composition;

using org.theGecko.Utilities.MefMvc;

using LeMigue.Scrum.Iterface.Model;

using LeMigue.TeamPortal.Base.Mvc;
using LeMigue.TeamPortal.Base.View;
using LeMigue.TeamPortal.Base.Biz.Interface;
using LeMigue.TeamPortal.Base.Biz.Implement;
using LeMigue.Scrum.Iterface.Enum;
using LeMigue.TeamPortal.Base.Biz;


namespace LeMigue.TeamPortal.ScrumBoard.Controllers
{
    [ControllerMetaData("Task")]
    [Export(typeof(IController)), PartCreationPolicy(CreationPolicy.NonShared)]
    [Authorize]
    [CheckTeam]
    public class TaskController : TeamPortalController
    {

        //TODO: implmentar tipo um unity
        private ITaskBiz TaskBiz = FactoryBiz.TaskBiz;


        [ChildActionOnly]
        public PartialViewResult TaskPostIt(TaskView task)
        {
            return PartialView(task);
        }

        [ChildActionOnly]
        public PartialViewResult LoadListSubTask(TaskView task, TaskState? state)
        {
            IList<TaskView> tasks = TaskBiz.List(task.Reference.Tasks, state);

            if (tasks.Count <= 0)
                return PartialView("Empty");

            return PartialView();
        }

        public PartialViewResult ListSubTask(int? id, int? extra1)
        {
            TaskState? state = extra1.HasValue ? (TaskState?)extra1 : null;
            TaskView task = TaskBiz.Load(id);
            IList<TaskView> tasks = TaskBiz.List(task.Reference.Tasks, state);

            if (tasks.Count <= 0)
                return PartialView("Empty");

            return PartialView(tasks);
        }

        [HttpGet]
        public PartialViewResult EditTask(int? id)
        {
            return PartialView(TaskBiz.Load(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        public PartialViewResult SalvarTask(Task taskSave)
        {
            //TODO: melhorar um pouco aqui também
            Task task;
            if(taskSave.Id > 0)
                task = TaskBiz.Save(taskSave);
            else
                task = TaskBiz.Create(taskSave,new ScrumBoardBiz().SprintAtivo(Team, 0).Iteration,Team);

            return PartialView("EditTask", new TaskView(taskSave));
        }

        [HttpGet]
        public PartialViewResult NewTask(int? id)
        {
            //TODO: melhorar isto aqui também
            TaskView task = TaskBiz.New(id);

            return PartialView("EditTask", task);
        }
    }
}
